<?php
/**
 *
 * @author yupoxiong<i@yupoxiong.com>
 */

declare (strict_types=1);

namespace app\admin\traits;

use support\Response;
use think\db\exception\DbException;

trait AdminIndexTrait
{
    /**
     * 列表
     * @return Response
     */
    public function index(): Response
    {
        $request = request();
        $model   = $this->model;

        $param = ($request)->all();
        try {
            $data = $model->scope('where', $param)
                ->paginate([
                    'list_rows' => $this->admin['admin_list_rows'],
                    'var_page'  => 'page',
                    'query'     => ($request)->get()
                ]);
        } catch (DbException $e) {
            $data = [];
        }

        // 关键词，排序等赋值
        $this->assign(($request)->get());

        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);

        return $this->fetch();
    }
}
